var layer;
var form;
var table;
var $;
layui.use([ 'layer', 'form', 'table', 'jquery' , 'laydate', 'upload' ], function() {
	layer = layui.layer;
	form = layui.form;
	table = layui.table;
	$ = layui.jquery; 

	//刷新地图
	$('.refresh_dt').click(function(){
		createMap('', $("#lng").val(), $("#lat").val());
	})
})

var localMark = 0;
function getLocal(){
	if(localMark == 1){
		return;
	}
	localMark = 1;
	map = new BMap.Map("l-map");
	var point = new BMap.Point(116.331398,39.897445);
	map.centerAndZoom(point,12);
	var myCity = new BMap.LocalCity();
	myCity.get(myFun); 
}
function myFun(result){
    var cityName = result.name;
    map.setCenter(cityName);
    //alert("当前定位城市:"+cityName);
    createMap(cityName);
}
/**
 * 百度地图
 */
//初始化
var map;
var marker;
function createMap(addr,lng,lat){
	$("#l-map").height("800px");
	$("#tips_dt").css("display","none");
	$("#l-map").css("display","block");
	$(".refresh_dt").css("display","block");
	// 百度地图API功能
	map = new BMap.Map("l-map");  // 创建Map实例
	//console.log(addr+"=="+lng+"=="+lat)
	if(lng&&lat){
		map.centerAndZoom(new BMap.Point(lng,lat),18);
		createPoint(lng,lat);
	}else{
		map.centerAndZoom(addr,18);      // 初始化地图,用城市名设置地图中心点
		var localSearch = new BMap.LocalSearch(map);
		localSearch.enableAutoViewport(); //允许自动调节窗体大小
		localSearch.setSearchCompleteCallback(function (searchResult) {
			var poi = searchResult.getPoi(0);
			createPoint(poi.point.lng,poi.point.lat);
			$("#lng").val(poi.point.lng)
			$("#lat").val(poi.point.lat)
		});
		localSearch.search(addr);
	}
	map.addEventListener("click", showInfo);
	map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
	map.addControl(new BMap.MapTypeControl({
		mapTypes:[
            BMAP_NORMAL_MAP,
            BMAP_HYBRID_MAP
        ]}));
	var ac = new BMap.Autocomplete( // 搜索
			{"input" : "suggestId"
			,"location" : map
			});
	ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
		var str = "";
		var _value = e.fromitem.value;
		var value = "";
		if (e.fromitem.index > -1) {
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
		value = "";
		if (e.toitem.index > -1) {
			_value = e.toitem.value;
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
		$("#searchResultPanel").html(str);
	});
	
	ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
		var _value = e.item.value;
		myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		$("#searchResultPanel").html("onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue);
		setPlace();
	});
		
}
var myValue;
function setPlace(){
	map.clearOverlays();    //清除地图上所有覆盖物
	function myFun(){
		var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
		$("#lng").val(pp.lng)
		$("#lat").val(pp.lat)
		map.centerAndZoom(pp, 18);
		createPoint(pp.lng,pp.lat);
	}
	var local = new BMap.LocalSearch(map, { //智能搜索
	  onSearchComplete: myFun
	});
	local.search(myValue);
}
function showInfo(e){//获取点击的经度纬度
	$("#lng").val(e.point.lng)
	$("#lat").val(e.point.lat)
	var myGeo = new BMap.Geocoder();
	var point = new BMap.Point(e.point.lng, e.point.lat);
 
	myGeo.getLocation(point, function(rs){
		var addComp = rs.addressComponents;
		var address = addComp.province+addComp.city+addComp.district+addComp.street+addComp.streetNumber;
		$("#suggestId").val(address)
	});
	createPoint(e.point.lng,e.point.lat);
}
function createPoint(lng,lat){
	if(marker){
		map.removeOverlay(marker)
	}
	var poi = new BMap.Point(lng,lat);
	marker = new BMap.Marker(poi); //创建marker对象
	marker.enableDragging(); //marker可拖拽
	marker.addEventListener("click", function(e){
		searchInfoWindow.open(marker);
	})
	marker.addEventListener("dragend",function(s){
		$("#lng").val(s.point.lng)
		$("#lat").val(s.point.lat)
	});
	map.addOverlay(marker); //在地图中添加marker
}